package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.RolePermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.RolePermission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@DubboService
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;
    /**
     * 权限分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        Example example = new Example(Permission.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("name", "%" + queryString + "%");
            criteria.orLike("keyword", "%" + queryString + "%");
        }
        //使用分页插件
        Page<Permission> permissions = (Page<Permission>) permissionDao.selectByExample(example);
        //获取当前页对应的数据
        List<Permission> result = permissions.getResult();
        log.info("[权限-分页]Permission{}", result);
        //获取总条数
        long total = permissions.getTotal();

        return new PageResult(total, result);
    }

    /**
     * 权限信息--->添加
     *
     * @param permission
     */
    @Override
    @Transactional
    public void add(Permission permission, Integer[] roleIds) {
        //添加权限表
        permissionDao.insertSelective(permission);
        //再次添加中间关系表
        Permission permission1 = permissionDao.selectOne(permission);
        for (Integer roleId : roleIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permission1.getId());
            rolePermissionDao.insertSelective(rolePermission);
        }
    }

    /**
     * 根据id查询权限信息,用户分页查询
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return  permissionDao.selectByPrimaryKey(id);

    }


    /**
     * 权限信息-修改数据
     * @param permission
     */
    @Override
    public void edit(Permission permission , Integer[] id) {
        //修改权限信息
        permissionDao.updateByPrimaryKeySelective(permission);
        //删除中间关联关系表中数据然后重新添加
        RolePermission rolePermission1 = new RolePermission();
        rolePermission1.setPermissionId(permission.getId());
        rolePermissionDao.delete(rolePermission1);
        for (Integer integer : id) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setPermissionId(permission.getId());
            rolePermission.setRoleId(integer);
            rolePermissionDao.insertSelective(rolePermission);
        }

    }

    /**
     * 根据id删除权限信息
     * @param id
     */
    @Override
    public void findDeleteById(Integer id) {
        permissionDao.deleteByPrimaryKey(id);
    }

    /**
     * 查询所有角色
     * @return
     */

    @Override
    public List<Role> findAll() {
        return roleDao.selectAll();
    }

    /**
     * 权限编辑--用于回显当前权限所对应的角色
     * @param id
     * @return
     */
    @Override
    public List<Integer> findByRole(Integer id) {
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(id);
        List<RolePermission> permissionList = rolePermissionDao.select(rolePermission);
        List<Integer> roleList = new ArrayList<>();
        for (RolePermission permission : permissionList) {
            roleList.add(permission.getRoleId());
        }
        return roleList;
    }
}